export default {
	namespaced:true,
	state:()=>({
		cart:[
			{
				goods_id:1,
				goods_name:'电动牙刷电动牙刷电动牙刷电动牙刷',
				goods_price:200,
				goods_origin_price:250,
				goods_count:1,
				goods_purchase_limit:10,
				goods_logo:'',
				goods_state:true,
				goods_type:'秒杀',
				goods_stock:10,
				goods_efficacy:true,//商品是否有效，用于清除商品
				goods_describe:'美味香椿嫩芽'
			},{
				goods_id:2,
				goods_name:'手动牙刷',
				goods_price:100,
				goods_origin_price:150,
				goods_count:1,
				goods_purchase_limit:10,
				goods_logo:'',
				goods_state:false,
				goods_type:'拼团',
				goods_stock:10,
				goods_efficacy:true,
				goods_describe:'美味香椿嫩芽'
			},{
				goods_id:4,
				goods_name:'牙膏',
				goods_price:100,
				goods_origin_price:150,
				goods_count:1,
				goods_purchase_limit:10,
				goods_logo:'',
				goods_state:true,
				goods_type:'秒杀',
				goods_stock:10,
				goods_efficacy:false,
				goods_describe:'美味香椿嫩芽'
			},{
				goods_id:5,
				goods_name:'杯子',
				goods_price:100,
				goods_origin_price:150,
				goods_count:1,
				goods_purchase_limit:10,
				goods_logo:'',
				goods_state:true,
				goods_type:'拼团',
				goods_stock:0,
				goods_efficacy:false,
				goods_describe:'美味香椿嫩芽'
			},
		],
		// cart:JSON.parse(uni.getStorageSync('cart')|| '[]'),
	}),
	mutations:{
		/* 添加商品到购物车 */
		addToCart(state,value){
			const findResult = state.cart.find( item => item.goods_id === value.goods_id)
			//判断购车车中是否已经有该商品
			if(findResult){
				findResult.goods_count++
			}else {
				state.cart.push(value)
			}
			this.commit('m_cart/saveToStorage')
		},
		/* 修改商品勾选状态 */
		updateGoodsState(state,value){
			const findResult = state.cart.find( item => item.goods_id === value.goods_id)
			if(findResult){
				findResult.goods_state = value.goods_state
			}
			this.commit('m_cart/saveToStorage')
		},
		/* 修改商品数量 */
		updateGoodsCount(state,value){
			const findResult = state.cart.find( item => item.goods_id === value.goods_id)
			if(findResult){
				findResult.goods_count = value.goods_count
			}
			this.commit('m_cart/saveToStorage')
		},
		/* 滑动删除商品 */
		removeGoodsById(state,value){
			// console.log(value);
			state.cart = state.cart.filter( item => item.goods_id !== value)
			this.commit('m_cart/saveToStorage')
		},
		/* 商品全选or取消全选 */
		updateAllGoodsState(state,value){
			state.cart.forEach( item => item.goods_state = value)
			this.commit('m_cart/saveToStorage')
		},
		/* 删除选中的商品 */
		removeCheckedGoods(state){
			state.cart = state.cart.filter( item => !item.goods_state)
			this.commit('m_cart/saveToStorage')
		},
		/* 删除所有失效商品 */
		removeAllLoseGoods(state){
			state.cart = state.cart.filter( item => item.goods_efficacy)
			this.commit('m_cart/saveToStorage')
		},
		/* 将购物车中的数据永久性存储到本地 */
		saveToStorage(state){
			uni.setStorageSync('cart',JSON.stringify(state.cart))
		}
	},
	actions:{
		/* 添加商品到购物车 */
		addToCart(context,value){
			context.commit('addToCart',value)
		},
		/* 修改商品勾选状态 */
		updateGoodsState(context,value){
			context.commit('updateGoodsState',value)
		},
		/* 修改商品数量 */
		updateGoodsCount(context,value){
			context.commit('updateGoodsCount',value)
		},
		/* 滑动删除商品 */
		removeGoodsById(context,value){
			context.commit('removeGoodsById',value)
		},
		/* 商品全选or取消全选 */
		updateAllGoodsState(context,value){
			context.commit('updateAllGoodsState',value)
		},
		/* 删除选中的商品 */
		removeCheckedGoods(context){
			context.commit('removeCheckedGoods')
		},
		/* 删除所有失效商品 */
		removeAllLoseGoods(context){
			context.commit('removeAllLoseGoods')
		}
	},
	getters:{
		/* 获取购物车中商品总数量 */
		total(state){
			 let c = 0
			return state.cart.filter( item => item.goods_efficacy)
							 .reduce((total,item) => total+=item.goods_count,0)
		},
		/* 勾选商品的总数量 */
		checkedCount(state){
			return state.cart.filter( item => item.goods_state & item.goods_efficacy)
							 .reduce((total,item) => total+=item.goods_count,0)
		},
		/* 已勾选商品的总价格 */
		checkedGoodsAmount(state){
			return state.cart.filter( item => item.goods_state & item.goods_efficacy)
							 .reduce((total,item) => total+=item.goods_price * item.goods_count,0)
							 .toFixed(2)
		},
		/* 已勾选商品节省的价格 */
		saveAmount(state){
			let originPrice = state.cart.filter( item => item.goods_state & item.goods_efficacy)
					  .reduce((total,item) => total += item.goods_origin_price * item.goods_count,0)
					  .toFixed(2)
					  return originPrice
		}
	}
}